﻿<!--首先导入一些必要的packages-->
<%@ page contentType="text/html; charset=utf-8" language="java" pageEncoding="utf-8"%>
<%@ page import="java.io.*, java.util.*,java.sql.*" %>
<%
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	response.setContentType("text/html; charset=utf-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Test Connect to database</title>
</head>
<body>
	<h1 align="center">This is a test page for connect to database!</h1>
	<%
		try
		{
			Connection con;
			Statement stmt;
			PreparedStatement preStmt;
			ResultSet rs;
			Class.forName("com.mysql.jdbc.Driver");
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			String dbUrl = "jdbc:mysql://localhost:3306/class1_1?useUnicode=true&characterEncoding=utf-8";
			String dbUser = "caifeifei";
			String dbPwd = "112997";
			con = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPwd);
			stmt = con.createStatement();
			preStmt = con.prepareStatement("select role_name from t_role where role_id=?");
			rs = stmt.executeQuery("select uid, uname, password, urole from t_user");
			
			out.println("<table border=1 width=700 noWrap>");
			out.println("<tr><td align='center'><b>用户Id</b></td><td align='center'><b>用户名</b></td><td align='center'><b>密码</b></td><td align='center'><b>角色</b></td></tr>");
			while(rs.next())
			{
				int userId = rs.getInt("uid");
				String userName = rs.getString("uname");
				String userPwd = rs.getString("password");
				int userRole = rs.getInt("urole");
				String strRole = "";
				/*
				switch(userRole)
				{
					case 0: strRole = "老师"; break;
					case 1: strRole = "学生"; break;
					case 2: strRole = "家长"; break;
					case -1: strRole = "管理员"; break;
					case -2: strRole = "游客"; break;
					default: strRole ="未定义"; break;
				}
				out.println("<tr><td noWrap>" + userId + "</td><td noWrap>" + userName + "</td><td noWrap>" + userPwd + "</td><td noWrap>" + strRole + "</td></tr>");
				*/				
				preStmt.setInt(1, userRole);
				ResultSet rs2 = preStmt.executeQuery();
				rs2.next();
				out.println("<tr><td noWrap>" + userId + "</td><td noWrap>" + userName + "</td><td noWrap>" + userPwd + "</td><td noWrap>" + rs2.getString("role_name") + "</td></tr>");
				rs2.close();
			}
			out.println("</table>");
			stmt.close();
			con.close();
		}
		catch(Exception e)
		{
			out.println(e.getMessage());
			out.println("<h2>Happen some errors!</h2>");
		}
	%>
</body>
</html>